package in.muton.panels.paginator.badgehelpers.course;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.panels.paginator.helpers.BadgeInterface;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.PrerequisiteLink;
import com.edushare.common.vo.SemesterCourses;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class CourseBadgeInterface implements BadgeInterface {

	public CourseBadgeInterface(Departments dept) {
		this.dept = new CayenneModel<Departments>(dept);
	}

	public CourseBadgeInterface(Universities univ) {
		this.univ = new CayenneModel<Universities>(univ);
	}

	public String removeCourse(IModel<Courses> cinst) {
		if (cinst.getObject().getDependentCourseLink().size() > 0) {
			PrerequisiteLink prl = cinst.getObject().getDependentCourseLink()
					.get(0);
			Courses c = prl.getCourse();
			return "This course is a prerequisite for some courses, including "
					+ c.getCourseName() + ". Unable to delete.";
		}
		if(cinst.getObject().getSemesterCourses().size() >0){
			SemesterCourses sc = cinst.getObject().getSemesterCourses().get(0);
			Curricula c = sc.getCurricula();
			return "This course is a part of academic program "+ c.getCurriculaName()
				+". Unable to delete";
		}
		CoursesDAO cDAO = new CoursesDAO(EduShareSession.get()
				.getSessionORMInterface());
		cDAO.removeCourse(cinst.getObject());
		return "";
	}

	public boolean isAdmin() {
		Users user = EduShareSession.get().getLoggedInUser();
		if (dept != null)
			return DeptRoleCheck.isPrimaryAdmin(dept.getObject(), user)||
				DeptRoleCheck.isUnivAdmin(dept.getObject(), user);
		if (univ != null)
			return UnivRoleCheck.isAdmin(univ.getObject(), user);
		return false;
	}

	private IModel<Universities> univ = null;
	private IModel<Departments> dept = null;
	private static final long serialVersionUID = -3285714919079363067L;
}
